<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <link rel="stylesheet" href="./css/jquery-ui.css">
    <script type="text/javascript" src="./js/jquery.min.js"></script>
    <script src="./js/common.js"></script>
    <script src="./js/jquery-ui.js"></script>
    <script>
        (function ($) {
            $.getUrlParam = function (name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }
        })(jQuery);
    </script>
    <script>
        var baseUrl = parent.baseUrl;
        var env = $.getUrlParam("env");
        var gateway = $.getUrlParam("gateway");
        var id = $.getUrlParam("id");
        var routeDomainIdDef = 0;
        var routeDomainDomainDef = '';

        var routeDomainDatas = [];

        $(function () {
            $("#l_id").val(id);

            $.ajax({
                url: baseUrl + "/servcieGateway/routeService/list/?env=" + env + "&gateway=" + gateway + "&applicationName=" + gateway + "&status=1",
                data: "",
                contentType: 'application/json;charset=utf-8',
                dataType: "JSON",
                async: false,
                type: "GET",
                success: function (respData) {
                    if (respData.respCode === '0000') {
                        var routeServcieDatas = respData.data.items;
                        var serviceIds = [];
                        if (routeServcieDatas) {
                            routeServcieDatas.forEach(function (_val, _ind) {
                                serviceIds.push(_val.id + "$" + _val.comment);
                            });

                            $("#l_service_id").autocomplete({
                                source: serviceIds
                            });
                        }
                    } else {
                        alert(respData.respDesc);
                    }
                }
            });

            $.ajax({
                url: baseUrl + "/servcieGateway/routeDomain/list/?env=" + env + "&gateway=" + gateway + "&applicationName=" + gateway + "&status=1",
                data: "",
                contentType: 'application/json;charset=utf-8',
                dataType: "JSON",
                async: false,
                type: "GET",
                success: function (respData) {
                    if (respData.respCode === '0000') {
                        routeDomainDatas = respData.data.items;
                        var domains = [];
                        if (routeDomainDatas) {
                            routeDomainDatas.forEach(function (_val, _ind) {
                                domains.push(_val.domain + "$" + _val.comment);
                            });

                            $("#l_domain").autocomplete({
                                source: domains
                            });
                        }
                    } else {
                        alert(respData.respDesc);
                    }
                }
            });


            $.ajax({
                url: baseUrl + "/servcieGateway/route/detail/" + id + "/?env=" + env + "&gateway=" + gateway + "&applicationName=" + gateway,
                data: "",
                contentType: 'application/json;charset=utf-8',
                dataType: "JSON",
                async: false,
                type: "GET",
                success: function (respData) {
                    if (respData.respCode === '0000') {
                        var routeData = respData.data;
                        $("#l_id").val(routeData.id);
                        $("#l_path").val(routeData.path);
                        $("#l_service_id").val(routeData.routeService.id);
                        routeDomainIdDef = routeData.routeDomain.id;
                        routeDomainDomainDef = routeData.routeDomain.domain;
                        $("#l_domain").val(routeData.routeDomain.domain);

                        if (routeData.stripPrefix == 'true' || routeData.stripPrefix == true) {
                            $("#l_strip_prefix").attr("checked", "");
                        }

                        if (routeData.addHostHeader == 'true' || routeData.addHostHeader == true) {
                            $("#l_addhost_header").attr("checked", "");
                        }

                        $("#l_sensitive_headers").val(routeData.sensitiveHeaders);
                        $("#l_sort").val(routeData.sort);
                        $("#l_comment").val(routeData.comment);

                        $("#l_status").find("option[value='" + routeData.statusCode + "']").attr("selected", "");

                    } else {
                        alert(respData.respDesc);
                    }
                }
            });


        });
    </script>

    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="./js/html5.min.js"></script>
    <script src="./js/respond.min.js"></script>
    <![endif]-->


</head>

<body>
<div class="x-body layui-anim layui-anim-up">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="l_id" class="layui-form-label">
                <span class="x-red">*</span>ID
            </label>
            <div class="layui-input-inline">
                <input type="text" id="l_id" name="id" required="" lay-verify="required" readonly value=""
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="l_path" class="layui-form-label">
                <span class="x-red">*</span>路径
            </label>
            <div class="layui-input-inline">
                <input type="text" id="l_path" name="path" required="" lay-verify="required"
                       autocomplete="off" placeholder="例如:/user/**" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>转发路径，必须要以/开头
            </div>
        </div>

        <div class="layui-form-item">
            <label for="l_service_id" class="layui-form-label">
                <span class="x-red">*</span>组ID
            </label>
            <div class="layui-input-inline">
                <input type="text" id="l_service_id" name="serviceId" required="" lay-verify="required"
                       value=""
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>组ID，$后面是备注信息，保存时会自动去除;输入$自动提示
            </div>
        </div>

        <div class="layui-form-item">
            <label for="l_domain" class="layui-form-label">
                域名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="l_domain" name="domain"
                       value=""
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                域名，$后面是备注信息，保存时会自动去除;输入$自动提示;可空
            </div>
        </div>


        <div class="layui-form-item">
            <label for="l_strip_prefix" class="layui-form-label">去除前缀路径</label>
            <div class="layui-input-inline">
                <input id="l_strip_prefix" type="checkbox" name="stripPrefix" lay-skin="switch"
                       lay-filter="switchStripPrefix" class="switch-input"
                       lay-text="是|否">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>例如/user/**,即转发时会去掉/user;默认去除
            </div>
        </div>

        <div class="layui-form-item">
            <label for="l_strip_prefix" class="layui-form-label"><span class="x-red">*</span>添加真实访问来源HOST</label>
            <div class="layui-input-inline">
                <input id="l_addhost_header" type="checkbox" name="addHostHeader" lay-skin="switch"
                       lay-filter="switchAddHostHeader" class="switch-input"
                       lay-text="是|否">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>添加真实访问来源的HOST;默认不添加
            </div>
        </div>

        <div class="layui-form-item">
            <label for="l_sensitive_headers" class="layui-form-label">
                敏感头信息内容
            </label>
            <div class="layui-input-inline">
                <textarea id="l_sensitive_headers" name="sensitiveHeaders"
                          class="layui-textarea"
                          style="height: 100px;"></textarea>
            </div>
            <div class="layui-form-mid layui-word-aux">
                多个以英文逗号分隔;默认没有敏感头。<br/>
                例如:Access-Control-Allow-Origin,Access-Control-Allow-Credentials
            </div>
        </div>

        <div class="layui-form-item">
            <label for="l_sort" class="layui-form-label">
                <span class="x-red">*</span>排序
            </label>
            <div class="layui-input-inline">
                <input type="text" id="l_sort" name="sort" required="" lay-verify="required" value=""
                       autocomplete="off" placeholder="例如:1.0" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>越小的优先级越高，精确到小数点后面2位;+/-999.99
            </div>
        </div>

        <!--<div class="layui-form-item">-->
        <!--<label for="l_retryable" class="layui-form-label">重试</label>-->
        <!--<div class="layui-input-block">-->
        <!--<input id="l_retryable" type="checkbox" name="retryable" lay-skin="switch"-->
        <!--lay-filter="switchRetryable" class="switch-input"-->
        <!--lay-text="是|否">-->
        <!--</div>-->
        <!--<div class="layui-form-mid layui-word-aux">-->
        <!--<span class="x-red">*</span>转发路径，必须要以/开头-->
        <!--</div>-->
        <!--</div>-->

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"> <span class="x-red">*</span>状态</label>
                <div class="layui-input-inline">
                    <select id="l_status" name="status">
                        <option value="1">可用</option>
                        <option value="0">不可用</option>
                    </select>
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <label for="l_comment" class="layui-form-label">
                <span class="x-red">*</span>备注
            </label>
            <div class="layui-input-inline">
                <input type="text" id="l_comment" name="comment" required="" lay-verify="required"
                       autocomplete="off" placeholder="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="" class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="edit" lay-submit="">
                修改
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;

        //监听提交
        form.on('submit(edit)', function (data) {
            var id = $.trim(data.field.id);
            var path = $.trim(data.field.path);
            var serviceId = $.trim(data.field.serviceId);
            serviceId = serviceId.split("$")[0];

            var domain = $.trim(data.field.domain);
            domain = domain.split("$")[0];

            var domainRouteId = 0;

            if (routeDomainDatas) {
                routeDomainDatas.forEach(function (_val, _ind) {
                    if (_val.domain === domain) {
                        domainRouteId = _val.id;
                    }
                });
            }

            if (domain === routeDomainDomainDef) {
                domainRouteId = routeDomainIdDef;
            }

            var stripPrefix = $.trim(data.field.stripPrefix);
            if (stripPrefix === 'on') {
                stripPrefix = true;
            } else {
                stripPrefix = false;
            }
            var addHostHeader = $.trim(data.field.addHostHeader);
            if (addHostHeader === 'on') {
                addHostHeader = true;
            } else {
                addHostHeader = false;
            }
            var sensitiveHeaders = $.trim(data.field.sensitiveHeaders);
            if (!sensitiveHeaders) {
                sensitiveHeaders = '';
            }
            var sort = $.trim(data.field.sort);
            var comment = $.trim(data.field.comment);
            var status = $.trim(data.field.status);

            var reqData = {
                request: {
                    id: id,
                    comment: comment,
                    status: status,
                    serviceId: serviceId,
                    path: path,
                    domainRouteId: domainRouteId,
                    stripPrefix: stripPrefix,
                    addHostHeader: addHostHeader,
                    sensitiveHeaders: sensitiveHeaders,
                    sort: sort
                }
            };

            $.ajax({
                url: baseUrl + "/servcieGateway/route/update?env=" + env + "&gateway=" + gateway,
                data: JSON.stringify(reqData),
                contentType: 'application/json;charset=utf-8',
                dataType: "JSON",
                async: false,
                type: "POST",
                success: function (respData) {
                    if (respData.respCode === '0000') {
                        layer.alert("修改成功", {icon: 6}, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);

                            parent.window.location.reload();
                        });
                    } else {
                        layer.msg(respData.respDesc, {
                            icon: 2,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            //do something
                        });
                    }
                }
            });

            return false;
        });


    });
</script>
<script>var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>